import React, { Component } from 'react'
import { message, Modal, Tree } from 'antd';
import { changeRolesAuthAPI } from '../apis/characterAPI';
import { getStorage } from './../utils/storageUtils'
const treeData = [
    {
        title: '首页',
        key: '/home/main',
    },
    {
        title: '用户管理',
        key: '/home/user',
    },
    {
        title: '角色管理',
        key: '/home/character',
    },
    {
        title: '店铺管理',
        key: '/home/shop',
    },
    {
        title: '商品管理',
        key: '/home/product',
        children: [
            {
                title: '品种管理',
                key: '/home/product/category'
            },
            {
                title: '商品列表',
                key: '/home/product/item'
            },
        ]
    },
    {
        title: '财务数据',
        key: '/home/datav',
        children: [
            {
                title: '流水信息',
                key: '/home/datav/flow'
            },
            {
                title: '交易数据',
                key: '/home/datav/deal'
            },
        ]
    },


]
export default class authModal extends Component {
    state = {
        checks: []
    }
    handleCancel = () => {
        this.props.showAuthModal(false)
    };
    treeCheck = (checks) => {
        console.log(checks);
        this.setState({ checks })
    }
    handleOk = async () => {
        // console.log(getStorage('userInfo'));
        const obj = {
            id: this.props.cid,
            authUser: getStorage('userInfo').role.authUser,
            authTime: new Date().toLocaleDateString(),
            menus: this.state.checks
        }
        const res = await changeRolesAuthAPI(obj)
        console.log('changeRolesAuthAPI()', res);
        if (res.data.code) {
            message.success('修改成功')
            this.props.getRoles()
            this.props.showAuthModal(false)
        } else {
            message.error('修改失败')
        }
    }
    render() {

        const { menus } = this.props
        return (
            <Modal title="授权" visible={true} onOk={this.handleOk} onCancel={this.handleCancel}>
                <Tree
                    checkable

                    defaultCheckedKeys={menus}
                    onCheck={this.treeCheck}
                    treeData={treeData}
                />
            </Modal>
        )
    }
}
